/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.date.DateUtil
 *  cn.hutool.core.util.ObjectUtil
 *  com.alibaba.fastjson.JSON
 *  de.felixroske.jfxsupport.FXMLController
 *  javafx.event.EventHandler
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.DatePicker
 *  javafx.scene.control.ScrollPane
 *  javafx.scene.control.Tab
 *  javafx.scene.control.TextField
 *  javafx.scene.input.KeyCode
 *  javafx.scene.input.KeyEvent
 *  javafx.scene.layout.GridPane
 *  javafx.util.StringConverter
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 *  org.springframework.beans.BeanUtils
 *  org.springframework.beans.factory.annotation.Autowired
 *  org.springframework.util.ObjectUtils
 */
package com.gys.controller.baseSetting.localSetting;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.gys.common.business.LocalSettingUtil;
import com.gys.common.data.HttpJson;
import com.gys.common.plugIn.dateTimePicker.DateTimePicker;
import com.gys.common.util.CacheUtil;
import com.gys.common.util.CommonUtil;
import com.gys.controller.BaseController;
import com.gys.controller.dataManger.ControllerManger;
import com.gys.controller.index.IndexController;
import com.gys.data.LocalSetting.LocalSettingInData;
import com.gys.data.LocalSetting.LocalSettingOutData;
import com.gys.data.example.GetLoginStoreOutData;
import com.gys.data.example.GetUserOutData;
import de.felixroske.jfxsupport.FXMLController;
import java.math.BigDecimal;
import java.net.URL;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.ResourceBundle;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.DatePicker;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tab;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import javafx.util.StringConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

@FXMLController
public class LocalSettingController
extends BaseController
implements Initializable {
    private static final Logger log = LoggerFactory.getLogger(LocalSettingController.class);
    @FXML
    private GridPane gridPane;
    @FXML
    private ScrollPane scrollPane;
    @FXML
    private DateTimePicker gsstDailyCloseDate;
    @FXML
    private DatePicker gsstPdStartDate;
    @FXML
    private DatePicker gsstJfQldate;
    @FXML
    private TextField gsstDyqMaxAmt;
    @FXML
    private DateTimePicker gsstDailyChangeDate;
    @FXML
    private TextField gsstBrName;
    @FXML
    private DateTimePicker gsstDailyOpenDate;
    @FXML
    private TextField gsstSaleMaxQty;
    @FXML
    private TextField gsstMinDiscountRate;
    @FXML
    private TextField gsstBrId;
    @FXML
    private TextField gsstPsDate;
    @Autowired
    private IndexController indexController;

    public void initialize(URL location, ResourceBundle resources) {
        ControllerManger.controllerMap.put(this.getClass().getSimpleName(), this);
        this.scrollPane.setPrefWidth(this.getContentWidth());
        this.scrollPane.setPrefHeight(this.getContentHeight());
        this.gridPane.setPrefWidth(this.getContentWidth());
        this.gridPane.setPrefHeight(this.getContentHeight());
        StringConverter converter = CommonUtil.dataPickerFormat("yyyyMMdd");
        this.gsstPdStartDate.setConverter(converter);
        this.gsstJfQldate.setConverter(converter);
    }

    @Override
    public void init() {
        this.indexController.scrollContentPane.setOnKeyReleased((EventHandler)new EventHandler<KeyEvent>(){

            public void handle(KeyEvent e) {
                if (e.getCode() == KeyCode.ENTER) {
                    LocalSettingController.this.save();
                } else if (e.getCode() == KeyCode.ESCAPE) {
                    LocalSettingController.this.cancel();
                }
                e.consume();
            }
        });
        this.gsstBrId.setText(((GetLoginStoreOutData)CacheUtil.get("store")).getStoCode());
        this.gsstBrName.setText(((GetLoginStoreOutData)CacheUtil.get("store")).getStoName());
        LocalSettingOutData outData = new LocalSettingOutData();
        if (!this.getSet(outData)) {
            return;
        }
        if (ObjectUtil.isEmpty(outData.getClientId())) {
            this.gsstDailyOpenDate.setTime("07:30");
            this.gsstDailyCloseDate.setTime("21:30");
            this.gsstDailyChangeDate.setTime("14:00");
            this.gsstSaleMaxQty.setText("99");
            this.gsstMinDiscountRate.setText("80");
            this.gsstPsDate.setText("7");
            this.gsstDyqMaxAmt.clear();
            this.gsstJfQldate.setValue(null);
            this.gsstPdStartDate.setValue(null);
            return;
        }
        this.gsstPsDate.setText(outData.getGsstPsDate());
        this.gsstBrId.setText(outData.getGsstBrId());
        this.gsstMinDiscountRate.setText(outData.getGsstMinDiscountRate());
        this.gsstSaleMaxQty.setText(outData.getGsstSaleMaxQty());
        this.gsstBrName.setText(outData.getGsstBrName());
        DecimalFormat decimalFormat = new DecimalFormat("##########.##########");
        this.gsstDyqMaxAmt.setText(decimalFormat.format(outData.getGsstDyqMaxAmt()));
        this.gsstDailyOpenDate.setTime(outData.getGsstDailyOpenDate());
        this.gsstDailyCloseDate.setTime(outData.getGsstDailyCloseDate());
        this.gsstDailyChangeDate.setTime(outData.getGsstDailyChangeDate());
        if (ObjectUtil.isNotEmpty(outData.getGsstPdStartDate())) {
            String pdsYear = outData.getGsstPdStartDate().substring(0, 4);
            String pdsMonth = outData.getGsstPdStartDate().substring(4, 6);
            String pdsDay = outData.getGsstPdStartDate().substring(6);
            this.gsstPdStartDate.setValue(LocalDate.of((int)Integer.valueOf(pdsYear), Integer.valueOf(pdsMonth), (int)Integer.valueOf(pdsDay)));
        }
        if (ObjectUtil.isNotEmpty(outData.getGsstJfQldate())) {
            String jFYear = outData.getGsstJfQldate().substring(0, 4);
            String jFMonth = outData.getGsstJfQldate().substring(4, 6);
            String jFDay = outData.getGsstJfQldate().substring(6);
            this.gsstJfQldate.setValue(LocalDate.of((int)Integer.valueOf(jFYear), Integer.valueOf(jFMonth), (int)Integer.valueOf(jFDay)));
        }
    }

    public boolean getSet(LocalSettingOutData outData) {
        LocalSettingInData inData = new LocalSettingInData();
        inData.setClientId(((GetUserOutData)CacheUtil.get("logInUser")).getClient());
        inData.setGsstBrId(((GetUserOutData)CacheUtil.get("logInUser")).getDepId());
        HttpJson httpJson = LocalSettingUtil.selectOne(CommonUtil.convertToMap(inData));
        if (!httpJson.isSuccess()) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", httpJson.getMsg());
            alert.showAndWait();
            return false;
        }
        LocalSettingOutData localSettingOutData = (LocalSettingOutData)JSON.parseObject((String)httpJson.getMsg(), LocalSettingOutData.class);
        BeanUtils.copyProperties(localSettingOutData, outData);
        return true;
    }

    @FXML
    private void save() {
        String message = this.check();
        if (!ObjectUtils.isEmpty(message)) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", message);
            alert.showAndWait();
            return;
        }
        LocalSettingInData gaiaSdStoreDataInData = new LocalSettingInData();
        gaiaSdStoreDataInData.setClientId(((GetUserOutData)CacheUtil.get("logInUser")).getClient());
        gaiaSdStoreDataInData.setGsstBrId(this.gsstBrId.getText().trim());
        gaiaSdStoreDataInData.setGsstBrName(this.gsstBrName.getText().trim());
        gaiaSdStoreDataInData.setGsstDailyChangeDate(this.gsstDailyChangeDate.getTime());
        gaiaSdStoreDataInData.setGsstDailyCloseDate(this.gsstDailyCloseDate.getTime());
        gaiaSdStoreDataInData.setGsstDailyOpenDate(this.gsstDailyOpenDate.getTime());
        gaiaSdStoreDataInData.setGsstPsDate(this.gsstPsDate.getText());
        if (ObjectUtil.isNotEmpty(this.gsstJfQldate.getValue())) {
            gaiaSdStoreDataInData.setGsstJfQldate(DateUtil.format((Date)DateUtil.parse((String)((LocalDate)this.gsstJfQldate.getValue()).toString()), (String)"yyyyMMdd"));
        }
        gaiaSdStoreDataInData.setGsstDyqMaxAmt(ObjectUtil.isEmpty(this.gsstDyqMaxAmt.getText().trim()) ? null : new BigDecimal(this.gsstDyqMaxAmt.getText().trim()));
        if (ObjectUtil.isNotEmpty(this.gsstPdStartDate.getValue())) {
            gaiaSdStoreDataInData.setGsstPdStartDate(DateUtil.format((Date)DateUtil.parse((String)((LocalDate)this.gsstPdStartDate.getValue()).toString()), (String)"yyyyMMdd"));
        }
        gaiaSdStoreDataInData.setGsstSaleMaxQty(this.gsstSaleMaxQty.getText().trim());
        gaiaSdStoreDataInData.setGsstMinDiscountRate(this.gsstMinDiscountRate.getText().trim());
        gaiaSdStoreDataInData.setGsstLastDay(DateUtil.format((Date)DateUtil.date(), (String)"yyyyMMdd"));
        HttpJson httpJson = LocalSettingUtil.saveOrUpDate(CommonUtil.convertToMap(gaiaSdStoreDataInData));
        CommonUtil.handleHttpResOnlyAlert(httpJson, "\u4fdd\u5b58\u6210\u529f");
    }

    public String check() {
        String message = "";
        if (ObjectUtils.isEmpty(this.gsstDailyOpenDate.getTime())) {
            message = "\u5f00\u5e97\u65f6\u95f4\u4e0d\u53ef\u4e3a\u7a7a";
        } else if (ObjectUtils.isEmpty(this.gsstDailyCloseDate.getTime())) {
            message = "\u95ed\u5e97\u65f6\u95f4\u4e0d\u53ef\u4e3a\u7a7a";
        } else if (this.gsstDailyOpenDate.getTime().compareTo(this.gsstDailyCloseDate.getTime()) > 0) {
            message = "\u5f00\u5e97\u65f6\u95f4\u4e0d\u80fd\u5927\u4e8e\u95ed\u5e97\u65f6\u95f4";
        } else if (ObjectUtils.isEmpty(this.gsstDailyChangeDate.getTime())) {
            message = "\u4ea4\u73ed\u65f6\u95f4\u4e0d\u53ef\u4e3a\u7a7a";
        } else if (ObjectUtils.isEmpty(this.gsstSaleMaxQty.getText().trim())) {
            message = "\u6bcf\u5355\u9500\u552e\u6700\u5927\u6570\u91cf\u4e0d\u53ef\u4e3a\u7a7a";
        } else if (ObjectUtils.isEmpty(this.gsstMinDiscountRate.getText().trim())) {
            message = "\u6700\u4f4e\u6298\u7387\u8bbe\u7f6e\u4e0d\u53ef\u4e3a\u7a7a";
        } else if (ObjectUtils.isEmpty(this.gsstDyqMaxAmt.getText().trim())) {
            message = "\u62b5\u7528\u5238\u6700\u5927\u4f7f\u7528\u91d1\u989d\u4e0d\u53ef\u4e3a\u7a7a";
        } else if (ObjectUtils.isEmpty(this.gsstPdStartDate.getValue())) {
            message = "\u5168\u573a\u76d8\u70b9\u5f00\u59cb\u65e5\u671f\u4e0d\u53ef\u4e3a\u7a7a";
        } else if (!CommonUtil.positiveInteger(this.gsstSaleMaxQty.getText().trim()) || this.gsstSaleMaxQty.getText().trim().length() > 20) {
            message = "\u6bcf\u5355\u9500\u552e\u6700\u5927\u6570\u91cf\u8bf7\u586b\u6b63\u6574\u6570\u4e14\u957f\u5ea6\u6700\u5927\u4e3a20";
        } else if (!CommonUtil.positiveInteger(this.gsstMinDiscountRate.getText().trim()) || this.gsstMinDiscountRate.getText().trim().length() > 10) {
            message = "\u6700\u4f4e\u6298\u7387\u8bbe\u7f6e\u8bf7\u586b\u6b63\u6574\u6570\u4e14\u957f\u5ea6\u6700\u5927\u4e3a10";
        } else if (!CommonUtil.positiveInteger(this.gsstDyqMaxAmt.getText().trim()) || this.gsstDyqMaxAmt.getText().trim().length() > 16) {
            message = "\u62b5\u7528\u5238\u6700\u5927\u4f7f\u7528\u91d1\u989d\u8bf7\u586b\u6b63\u6574\u6570\u4e14\u957f\u5ea6\u6700\u5927\u4e3a16";
        } else if (!CommonUtil.positiveInteger(this.gsstPsDate.getText().trim()) || this.gsstPsDate.getText().trim().length() > 2) {
            message = "\u8865\u8d27\u5929\u6570\u7ef4\u62a4\u8bf7\u586b\u6b63\u6574\u6570\u4e14\u957f\u5ea6\u6700\u5927\u4e3a2";
        }
        return message;
    }

    @FXML
    private void cancel() {
        Tab localInfo = this.indexController.tabMap.get("localSetting");
        this.indexController.tabPane.getTabs().remove(localInfo);
        this.indexController.tabMap.remove("localSetting");
        this.indexController.expandTitlePanes();
        this.indexController.getScrollContentPane().setOnKeyReleased(null);
    }
}

